package com.popo.http;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpURLConnectionTest {
	public static void main(String[] args) {

		try {
//			URL url = new URL("http://192.168.2.105");
			URL url = new URL("http://localhost");
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setConnectTimeout(2000);
			conn.setReadTimeout(15000);
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");
			String BOUNDARY = "41184676334---------------------------41184676334";
			conn.setRequestProperty("Context-type",	"multipart/form-data; boundary=" + BOUNDARY);
//			conn.setChunkedStreamingMode(1024 * 50);

			StringBuffer params = new StringBuffer();

			params.append("language=chinese&country=china");

			byte[] bypes = params.toString().getBytes();

			DataOutputStream out  =  new DataOutputStream(conn.getOutputStream());
			out.write("\n\r".getBytes());
			//out.writeBytes("--" + BOUNDARY + "\r\n");
			out.write(bypes);// 输入参数
			// conn.getOutputStream().write(end_data);

//			StringBuffer sb = new StringBuffer();
//			// 发送文件:
//			sb = sb.append("--");
//			sb = sb.append(BOUNDARY);
//			sb = sb.append("\r\n");
//			sb = sb.append("Content-Disposition: form-data; name=\"s\"; filename=\"165247.txt\"\r\n");
//			sb = sb.append("Content-Type: application/octet-stream\r\n\r\n");
//			byte[] data = sb.toString().getBytes();
//			byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
//			
//			conn.getOutputStream().write(data);
//			conn.getOutputStream().write(f);
//			conn.getOutputStream().write(end_data);
			conn.getOutputStream().flush(); 
			int response = conn.getResponseCode();
			InputStream is = conn.getInputStream();
			try {
				System.out.println(getStringFromInputStrean(is));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static byte[] readFile(String file) throws Exception {
		FileInputStream fis = new FileInputStream(file);
		byte[] b = new byte[256];
		int l;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		while ((l = fis.read(b)) != -1) {
			bos.write(b, 0, l);
		}
		fis.close();
		return bos.toByteArray();
	}

	/**
	 * 刘立波添加从InputStream中读出流成字符串
	 * 
	 * @param in
	 * @return
	 * @throws Exception
	 */
	public static String getStringFromInputStrean(InputStream in)
			throws Exception {

		StringBuilder stringBuilder = new StringBuilder();
		try {

			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String line = "";
			while ((line = br.readLine()) != null) {
				stringBuilder.append(line);
			}
			br.close();
			br = null;
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw (e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw (e);
		} finally {
			try {
				in.close();
			} catch (Exception e) {
				// TODO: handle exception
				throw (e);
			}
		}
		return stringBuilder.toString().trim();
	}
}

class FileUtil {

	public byte[] getFileByte(String fileName) throws FileNotFoundException {
		FileInputStream fileInputStream = new FileInputStream(fileName);
		return getFileByte(fileInputStream);
	}

	public byte[] getFileByte(URL url) throws IOException {
		if (url != null) {
			return getFileByte(url.openStream());
		} else {
			return null;
		}
	}

	public byte[] getFileByte(InputStream in) {
		ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
		try {
			copy(in, out);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return out.toByteArray();

	}

	private void copy(InputStream in, OutputStream out) throws IOException {

		try {
			byte[] buffer = new byte[4096];
			int nrOfBytes = -1;
			while ((nrOfBytes = in.read(buffer)) != -1) {
				out.write(buffer, 0, nrOfBytes);
			}
			out.flush();
		} catch (IOException e) {

		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
			}
			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException ex) {
			}
		}

	}

}
